package com.hspedu.qqclient.view;

import com.hspedu.qqclient.Utils.Utility;
import com.hspedu.qqclient.service.FileClientservice;
import com.hspedu.qqclient.service.MessageClientService;
import com.hspedu.qqclient.service.UserClientService;

/**
 * @author 王先锋
 * @version 1.0
 * 客户端菜单界面
 */
public class QQView {
    private boolean loop = true;//控制是否显示菜单
    private String key = "";//接收用户键盘输入
    private UserClientService userClientService = new UserClientService();//对象用于登陆服务/注册用户
    private MessageClientService messageClientService = new MessageClientService();//对象用户私聊
    private FileClientservice fileClientservice = new FileClientservice();//该对象用于文件传输


    public static void main(String[] args) {
        new QQView().mainMenu();
        System.out.println("客户端退出系统...");
    }


    //显示菜单
    private void mainMenu() {
        while (loop) {
            System.out.println("==========欢迎登陆网络通信系统==========");
            System.out.println("\t\t 1 登陆系统");
            System.out.println("\t\t 9 退出系统");
            System.out.println("请输入你的选择");
            key = Utility.readString(1);//只能输入一个字符
            //根据用户的输入，来处理不同的逻辑
            switch (key) {
                case "1":
                    System.out.print("请输入用户号：");
                    String userId = Utility.readString(50);
                    System.out.print("请输入密  码：");
                    String pwd = Utility.readString(50);
                    //需要导服务端验证用户是否合法
                    if (userClientService.checkUser(userId, pwd)) {
                        System.out.println("==========欢迎" + userId + "登陆成功====");
                        //进入二级菜单
                        while (loop) {
                            System.out.println("\n==========网络通信系统二级菜单用户" + userId + "==========");
                            System.out.println("\t\t 1 显示在线用户列表");
                            System.out.println("\t\t 2 群发消息");
                            System.out.println("\t\t 3 私聊消息");
                            System.out.println("\t\t 4 发送文件");
                            System.out.println("\t\t 9 退出系统");
                            System.out.println("请输入你的选择 :");
                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    // System.out.println("显示在线用户列表");
                                    userClientService.onlineFriendList();
                                    break;
                                case "2":
                                    //System.out.println("群发消息");
                                    System.out.println("请输入想对大家说的话： ");
                                    String s1 = Utility.readString(100);
                                    //调用一个方法，将消息封装成message，发送给服务端
                                    messageClientService.sendMessageToAll(s1, userId);
                                    break;
                                case "3":
                                    //System.out.println("私聊消息");
                                    System.out.print("请输入想要聊天的用户号");
                                    String getterId = Utility.readString(50);
                                    System.out.println("请输入要说的话");
                                    String s = Utility.readString(100);
                                    //调用方法，发给服务器
                                    messageClientService.sendMessageToOne(s, userId, getterId);
                                    break;
                                case "4":
                                    //System.out.println("发送文件");
                                    System.out.println("请输入你想文件发给谁");
                                    getterId = Utility.readString(50);
                                    System.out.println("请输入发送文件路径");
                                    String src = Utility.readString(100);
                                    System.out.println("你要发送到对方那个路径");
                                    String dest = Utility.readString(100);
                                    fileClientservice.sendFileToOne(src,dest,userId,getterId);
                                    break;
                                case "9":
                                    //调用方法，给服务器发送一个退出系统的message
                                    userClientService.logout();
                                    loop = false;
                                    break;
                            }
                        }
                    } else {//登陆失败
                        System.out.println("登陆失败");
                    }
                    break;
                case "9":
                    loop = false;
                    break;
            }

        }
    }
}
